import type {CommandRegistryItem, CommandResult} from './command.type'
import {CommandRegistry} from './CommandRegistry'
import {CommandExecutor} from './CommandExecutor'
import {CommandFactory} from './CommandFactory'

// 命令总线（核心注册）
export class CommandCenter {
  private registry = new CommandRegistry()

  register(item: CommandRegistryItem): void {
    this.registry.register(item)
  }

  get(module: string, commandName: string): CommandRegistryItem | undefined{
    const item = this.registry.get(module, commandName)
    return item;
  }

  async execute(module: string, commandName: string, ...params: any[]): Promise<CommandResult> {
    const item = this.registry.get(module, commandName)
    if (!item) {
      return {
        success: false,
        message: `Command not found: ${module}.${commandName}`
      }
    }
    const command = CommandFactory.create(item, params)
    return CommandExecutor.execute(command, ...params)
  }
}